package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Sound;
import com.tanhua.dubbo.server.service.IdService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * @author zwy
 * 桃花传音实现类
 */
@Service(version = "1.0.0")
public class SoundApiImpl implements SoundApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;


    /**
     * 功能描述: saveSound    保存声音
     *
     * @param sound
     * @Return: {@link String}
     * @throws:
     * @Author: zwy
     * @Date: 2020/12/22 20:29
     */
    @Override
    public String saveSound(Sound sound) {
        //如果sound对象的id为空,则返回null
        if (sound.getUserId() == null) {
            return null;
        }
        sound.setId(ObjectId.get());
        sound.setCreated(System.currentTimeMillis());

        //自增长id
        sound.setSid(this.idService.createdId("sound", sound.getId().toString()));
        this.mongoTemplate.save(sound);
        return sound.getId().toString();
    }

    /**
     * 功能描述: receiveSound   接收声音
     *
     * @param userId
     * @Return: {@link List<  Sound >}
     * @throws:
     * @Author: zwy
     * @Date: 2020/12/23 10:47
     */
    @Override
    public List<Sound> receiveSound(Long userId) {
        //将自己排除掉
        Query query = Query.query(Criteria.where("userId").ne(userId));
        return this.mongoTemplate.find(query, Sound.class);
    }

}
